#ifndef PERSIST_FILE_H_
#define PERSIST_FILE_H_

#include <string>

using namespace std;

typedef void (*ParseFunc)(const string &, void *);

class PersistFile
{
public:
	PersistFile();
	~PersistFile();

	bool Open(const string & name);
	void Close();
	int Persist(const char *data);

	static void LoadPersistFile(const string &name, ParseFunc pf, void *ctx);

	template<typename T>
	static int Persist(T *t, PersistFile *file) {
		char buf[T::Length+1];
		int len = T::set(buf, t);
		buf[len] = '\0';
		return file->Persist(buf);
	}
	template<typename T>
	static bool Parse(const char* data, int len, T* t) {
		return	len == T::Length && 
				T::get(&data, len, t);
	}
protected:
	bool			IsOpen;
	FILE			*File;
};

#endif